package net.ecpt.clm.services;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.poi.EncryptedDocumentException;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.wvtools.utils.ExcelPoiUtils;

import javolution.util.FastList;
import javolution.util.FastMap;


public class ClmCompanyXlsServices {
    public static final String module = ClmCompanyXlsServices.class.getName();

    /**
     * 导入
     */
    public static Map<String, Object> importXls(DispatchContext dctx, Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Delegator delegator = dctx.getDelegator();
	GenericValue userLogin = (GenericValue)context.get("userLogin");
	List<String> messageList = new ArrayList<String>();
	List<Map<String, Object>> sheetList = (List<Map<String, Object>>) context.get("sheetList");
	try {
	    int i = 0;
	    List<GenericValue> entityList = FastList.newInstance();
	    for (Map<String, Object> row : sheetList) {
		i = i + 1;
		String id = ExcelPoiUtils.getCellString(row, 0);
		String typeId = ExcelPoiUtils.getCellString(row, 1);
		String companyName = ExcelPoiUtils.getCellString(row, 2);
		String legalName = ExcelPoiUtils.getCellString(row, 3);
		String legalIdCard = ExcelPoiUtils.getCellString(row, 4);
		String taxNumber = ExcelPoiUtils.getCellString(row, 5);
		String invoiceAddress = ExcelPoiUtils.getCellString(row, 6);
		String invoicePhone = ExcelPoiUtils.getCellString(row, 7);
		String bankName = ExcelPoiUtils.getCellString(row, 8);
		String companyPhone = ExcelPoiUtils.getCellString(row, 9);
		String companyWebsite = ExcelPoiUtils.getCellString(row, 10);
		String levelId = ExcelPoiUtils.getCellString(row, 11);
		String industryId = ExcelPoiUtils.getCellString(row, 12);
		String detailedAddress = ExcelPoiUtils.getCellString(row, 13);
		String description = ExcelPoiUtils.getCellString(row, 14);

	
		if (UtilValidate.isEmpty(typeId)) {
		    messageList.add("第" + i + "行,公司类型不能为空。");
		    continue;
		}
		
		
		if (UtilValidate.isEmpty(companyName)) {
		    messageList.add("第" + i + "行,公司名称不能为空。");
		    continue;
		}
		if(UtilValidate.isEmpty(id)) {
		    id = delegator.getNextSeqId("ClmCompany");
		}
		//
		
		GenericValue company = delegator.makeValue("ClmCompany");
		company.set("id", id);
		company.set("typeId", typeId);
		company.set("legalName", legalName);
		company.set("legalIdCard", legalIdCard);
		company.set("companyName", companyName);
		company.set("taxNumber", taxNumber);
		company.set("invoiceAddress", invoiceAddress);
		company.set("invoicePhone", invoicePhone);
		company.set("bankName", bankName);
		company.set("companyPhone", companyPhone);
		company.set("companyWebsite", companyWebsite);
		company.set("levelId", levelId);
		company.set("industryId", industryId);
		company.set("detailedAddress", detailedAddress);
		company.set("description", description);
		company.set("createDate", UtilDateTime.nowTimestamp());

		company.set("createBy", userLogin.getString("partyId"));
		company.set("statusId", "0");
		company.set("legalStatusId", "LEGAL_APPLY");
		entityList.add(company);
//		company.create();

	    }
	    delegator.storeAll(entityList);
	    result.put("messageList", messageList);
	} catch (EncryptedDocumentException e) {
	    e.printStackTrace();
	} catch (GenericEntityException e) {
	    e.printStackTrace();
	}
	return result;
    }

    /**
     * 导出
     */

    public static Map<String, Object> exportXls(DispatchContext dctx, Map<String, ? extends Object> context) {
	Delegator delegator = dctx.getDelegator();
	java.text.DateFormat orderDateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	DecimalFormat df = new DecimalFormat("0.00");
	List<GenericValue> orderList = FastList.newInstance();
	String ids = (String) context.get("ids");
	List<String> baseIdList = FastList.newInstance();
	if (UtilValidate.isNotEmpty(ids)) {
	    baseIdList = Arrays.asList(ids.split(";"));
	}
	Map<String, Object> result = ServiceUtil.returnSuccess();

	EntityCondition mainCond = null;
	List<EntityCondition> andExprs = FastList.newInstance();

	if (UtilValidate.isNotEmpty(baseIdList))
	    andExprs.add(EntityCondition.makeCondition("id", EntityOperator.IN, baseIdList));
	if (andExprs.size() > 0)
	    mainCond = EntityCondition.makeCondition(andExprs, EntityOperator.AND);

	try {
	    orderList = delegator.findList("ClmCompany", mainCond, null, null, null, true);

	    List<Map<String, Object>> list = FastList.newInstance();
	    Map<String, Object> keyMap = FastMap.newInstance();
	    Map<String, Object> keyItemMap = FastMap.newInstance();
	    String headers = "公司编号,类型,公司名称,法人姓名,法人身份证号码,税号,开票地址,开票电话,开户银行,公司电话,公司官网,公司级别,所属行业,地址,备注,创建时间";
	    result.put("headers", headers);
	    for (GenericValue company : orderList) {
		Map<String, Object> map = FastMap.newInstance();
		map.put("id", company.getString("id"));
		keyMap.put("0", "id");
		map.put("typeId", company.getString("typeId"));
		keyMap.put("1", "typeId");
		map.put("companyName", company.getString("companyName"));
		keyMap.put("2", "companyName");
		map.put("legalName", company.getString("legalName"));
		keyMap.put("3", "legalName");
		map.put("legalIdCard", company.getString("legalIdCard"));
		keyMap.put("4", "legalIdCard");
		map.put("taxNumber", company.getString("taxNumber"));
		keyMap.put("5", "taxNumber");


		map.put("invoiceAddress", company.getString("invoiceAddress"));
		keyMap.put("6", "invoiceAddress");

		map.put("invoicePhone", company.getString("invoicePhone"));
		keyMap.put("7", "invoicePhone");

		map.put("bankName", company.getString("bankName"));
		keyMap.put("8", "bankName");

		map.put("companyPhone", company.getString("companyPhone"));
		keyMap.put("9", "companyPhone");

		map.put("companyWebsite", company.getString("companyWebsite"));
		keyMap.put("10", "companyWebsite");
		
		
		String levelId = company.getString("levelId");
		String levelDesc = ExcelPoiUtils.getDataDictionaryDescription(delegator, "AgreementType", levelId);
		map.put("levelId", levelDesc);
		keyMap.put("11", "levelId");
		
		String industryId = company.getString("industryId");
		String industryDesc = ExcelPoiUtils.getDataDictionaryDescription(delegator, "AgreementType", industryId);
		
		map.put("industryId", industryDesc);
		keyMap.put("12", "industryId");

		map.put("address", company.getString("address"));
		keyMap.put("13", "address");

		map.put("description", company.getString("description"));
		keyMap.put("14", "description");


		String createDate = "";
		if (UtilValidate.isNotEmpty(company.get("createDate"))) {
		    createDate = orderDateFormat.format(company.getTimestamp("createDate"));
		}
		//处理日期
		map.put("createDate", createDate);
		keyMap.put("15", "createDate");

		list.add(map);
	    }
	    result.put("list", list);
	    result.put("keyMap", keyMap);
	    result.put("keyItemMap", keyItemMap);//父子表才需要此字段
	} catch (GenericEntityException e) {
	    e.printStackTrace();
	}
	return result;
    }

}
